App({
  globalData: {
    baseUrl: 'http://localhost:8080/',
    cdnUrl: 'http://localhost:8080/',
    userInfo: null,
    isLogin: false,
    isRealNameAuth: false,
    isMember: false,
    memberExpireTime: null,
    userProfile: null,
    location: null
  },

  onLaunch() {
    // 检查登录状态
    this.checkLoginStatus();
    
    // 获取用户位置
    this.getLocation();
    
    // 检查更新
    this.checkUpdate();
  },

  // 检查登录状态
  checkLoginStatus() {
    const token = wx.getStorageSync('token');
    const userInfo = wx.getStorageSync('userInfo');
    if (token && userInfo) {
      this.globalData.isLogin = true;
      this.globalData.userInfo = userInfo;
      this.globalData.isRealNameAuth = userInfo.isRealNameAuth || false;
      this.globalData.isMember = userInfo.isMember || false;
      this.globalData.memberExpireTime = userInfo.memberExpireTime;
      this.globalData.userProfile = userInfo.profile;
    }
  },

  // 获取用户位置
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.globalData.location = {
          latitude: res.latitude,
          longitude: res.longitude
        };
      },
      fail: (err) => {
        console.log('获取位置失败:', err);
      }
    });
  },

  // 检查小程序更新
  checkUpdate() {
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate((res) => {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(() => {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: (res) => {
                if (res.confirm) {
                  updateManager.applyUpdate();
                }
              }
            });
          });
        }
      });
    }
  },

  // 登录方法
  login(userInfo) {
    this.globalData.userInfo = userInfo;
    this.globalData.isLogin = true;
    this.globalData.isRealNameAuth = userInfo.isRealNameAuth || false;
    this.globalData.isMember = userInfo.isMember || false;
    this.globalData.memberExpireTime = userInfo.memberExpireTime;
    this.globalData.userProfile = userInfo.profile;
    
    wx.setStorageSync('token', userInfo.token);
    wx.setStorageSync('userInfo', userInfo);
  },

  // 退出登录
  logout() {
    this.globalData.userInfo = null;
    this.globalData.isLogin = false;
    this.globalData.isRealNameAuth = false;
    this.globalData.isMember = false;
    this.globalData.memberExpireTime = null;
    this.globalData.userProfile = null;
    
    wx.removeStorageSync('token');
    wx.removeStorageSync('userInfo');
  },

  // 检查用户资料完整性
  checkProfileComplete() {
    const profile = this.globalData.userProfile;
    if (!profile) return false;
    
    const requiredFields = ['nickname', 'gender', 'birthYear', 'income', 'city', 'datingType', 'description'];
    return requiredFields.every(field => profile[field] && profile[field] !== '');
  },

  // 显示完善资料提示
  showProfileIncompleteTip() {
    wx.showModal({
      title: '提示',
      content: '完善个人信息查看更多有缘人',
      showCancel: false,
      confirmText: '去完善'
    });
  }
}); 